home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** File: Menu.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1993 Apple Computer, Inc.
- ** All rights reserved.
- */
-
- /* You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes. */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "App.h" /* Get the application includes/typedefs, etc. */
- #include "App.defs.h" /* Get various application definitions. */
- #include "App.protos.h" /* Get the prototypes for application. */
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern Boolean gQuitApplication;
- extern Boolean gHasAppleEvents;
- extern OSType gAppWindowType;
-
- extern Boolean gLowOnMem;
- extern short gDialogErr;
-
- static short NewFontSize(short size);
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* •• Called by DTS.Lib..framework. •• */
-
- /* Adjust the menu items. We allow the DTS.Lib framework to do most of the work
- ** for us. It will walk the menubar, and for each menu in the menubar, it will
- ** disable all of the menu items and then call the application at either
- ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
- ** or DialogAdjustMenuItems() (for modal dialogs). The application's job is to then
- ** turn on menu items that should be enabled to match the current application state.
- ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
- ** file menu, and DoAdjustEditMenu() for the edit menu. Any other menus that may
- ** be added to Wannabe have all menu items enabled. This allows menus to be added
- ** to the running version of Wannabe and allows them to actually do something.
- ** If the top-most window is a dialog, then all menus are disabled except for the
- ** edit menu. Various items in the edit menu are enabled, depending on if there
- ** is an active TextEdit control, and what is in the clipboard. */
-
- #pragma segment Menu
- void DoAdjustMenus(void)
- {
- if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
- DrawMenuBar();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This is called when an item is chosen from the menu bar (after calling
- ** MenuSelect or MenuKey). It performs the right operation for each command.
- ** It is good to have both the result of MenuSelect and MenuKey go to one
- ** routine like this to keep everything organized. */
-
- #pragma segment Menu
- Boolean DoMenuCommand(short menuID, short menuItem)
- {
- short undoDepth, numUndos, saveMode, daRefNum, mode, size;
- Str255 str;
- FileRecHndl frHndl, newFrHndl;
- WindowPtr window;
- TEHandle te;
- OSErr err;
- Boolean handled;
- TEHandle teHndl;
- TextStyle styl;
- MenuHandle menu;
- static Style stylVal[] = {0, 0, 0, 1, 2, 4, 8, 16, 32, 64};
-
- handled = true;
-
- window = FrontWindow();
- if (window)
- frHndl = (FileRecHndl)GetWRefCon(window);
- /* frHndl is valid only if it is one of our windows. */
-
- switch (menuID) {
-
- case mApple:
- switch (menuItem) {
- case kStdAbout: /* Bring up alert for About. */
- NewDocumentWindow(nil, 'ABOT', false);
- break;
- default: /* All non-About items in this menu are DAs. */
- GetMenuItemText(GetMenuHandle(mApple), menuItem, str);
- daRefNum = OpenDeskAcc(str);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case kStdNew:
- gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
- if (gDialogErr)
- NewDocumentWindow(nil, 'ERR#', false);
- break;
- case kStdOpen:
- err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kStdClose:
- if (IsAppWindow(window)) {
- window = FrontWindowOfType(kwIsDocument, true);
- if (window)
- DisposeOneWindow(window, kClose);
- }
- else
- DisposeOneWindow(window, kClose); /* Dispose of DA window. */
- break;
- case kStdSave:
- case kStdSaveAs:
- saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- saveMode = kSaveAs;
- err = SaveDocument(frHndl, window, saveMode);
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kDuplicate:
- gDialogErr = DuplicateDocument(frHndl, &newFrHndl);
- if (!gDialogErr) {
- gDialogErr = DoNewWindow(newFrHndl, nil, FrontWindowOfType(kwIsDocument, true), (WindowPtr)-1);
- if (gDialogErr)
- DisposeDocument(newFrHndl);
- }
- if (gDialogErr)
- NewDocumentWindow(nil, 'ERR#', false);
- break;
- case kStdPageSetup:
- DoSetCursor(&qd.arrow);
- PresentStyleDialog(frHndl);
- break;
- case kStdPrint:
- DoSetCursor(&qd.arrow);
- err = noErr;
- if (!(*frHndl)->d.doc.fhInfo.printRecValid)
- err = PresentStyleDialog(frHndl);
- if (!err) {
- err = PrintDocument(frHndl, true, true);
- PrintDocument(nil, false, false);
- }
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kStdQuit:
- gQuitApplication = DisposeAllWindows();
- break;
- default:
- handled = false;
- break;
- }
- break;
-
- case mEdit: /* Call SystemEdit for DA editing & MultiFinder. */
- #if VH_VERSION
- if (menuItem == kStdViewHier) {
- handled = false;
- break;
- }
- #endif
- if (IsAppWindow(window)) {
- switch (menuItem) {
- case kStdUndo:
- case kStdRedo:
- case kStdCut:
- case kStdCopy:
- case kStdPaste:
- case kStdClear:
- switch ((*frHndl)->fileState.sfType) {
- /* This is written with the assumption that document types
- ** that demand specific code will be added. The below “if”
- ** illustrates how to handle the edit menu for windows that
- ** have an active TextEdit control. The “else” shows a typical
- ** undo/redo scenario for applications that are using the
- ** hierarchical document package. The clipboard features
- ** are of course document-dependent, so a sample hasn't been
- ** implemented here. For a sample, see DTS.Draw. */
- default:
- te = CTEFindActive(window);
- if (te) {
- if ((*te)->viewRect.left < -8192)
- BeginFrame(window);
- else
- BeginContent(window);
- if (menuItem == kStdUndo)
- CTEUndo();
- else
- CTEClipboard(menuItem - kStdCut + 2);
- EndContent(window);
- }
- else {
- if (menuItem <= kStdRedo) {
- if (!UnmapMItem(mEdit, kStdUndo)) {
- GetUndoInfo(frHndl, &undoDepth, &numUndos);
- DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
- }
- else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
- }
- else {
- /* Handle rest of edit menu here. */
- }
- }
- break;
- }
- break;
- }
- }
- else SystemEdit(menuItem - 1);
- break;
-
- case mFonts:
- teHndl = CTEFindActive(nil);
- if (teHndl) {
- if (menuItem <= kExtend) {
- if ((*teHndl)->selStart != (*teHndl)->selEnd)
- CTENewUndo(CTEViewFromTE(teHndl), true);
- mode = (styl.tsFace = stylVal[menuItem]) ? (doFace | doToggle) : (doFace);
- TESetStyle(mode, &styl, true, teHndl);
- CTEAdjustTEBottom(teHndl);
- CTEAdjustScrollValues(teHndl);
- SetDocDirty(frHndl);
- }
- if (menuItem == kFontSize) {
- if ((*teHndl)->selStart != (*teHndl)->selEnd)
- CTENewUndo(CTEViewFromTE(teHndl), true);
- size = 0;
- mode = doSize;
- TEContinuousStyle(&mode, &styl, teHndl);
- if (mode)
- if (!(size = styl.tsSize))
- size = 12;
- styl.tsSize = NewFontSize(size);
- if (styl.tsSize) {
- TESetStyle(doSize, &styl, true, teHndl);
- CTEAdjustTEBottom(teHndl);
- CTEAdjustScrollValues(teHndl);
- SetDocDirty(frHndl);
- }
- }
- if (menuItem >= kFirstFont) {
- if ((*teHndl)->selStart != (*teHndl)->selEnd)
- CTENewUndo(CTEViewFromTE(teHndl), true);
- menu = GetMenuHandle(mFonts);
- GetMenuItemText(menu, menuItem, str);
- GetFNum(str, &styl.tsFont);
- TESetStyle(doFont, &styl, true, teHndl);
- CTEAdjustTEBottom(teHndl);
- CTEAdjustScrollValues(teHndl);
- SetDocDirty(frHndl);
- }
- }
- break;
-
- default:
- handled = false;
- break;
-
- }
-
- return(handled);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustFileMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl;
- short enableItem;
-
- menu = GetMenuHandle(mFile);
- EnableItem(menu, UnmapMItem(mFile, kStdQuit)); /* Gotta be able to quit. */
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose)); /* Let DAs do a close from the menu. */
- return(false);
- }
-
- if (!gLowOnMem) {
- EnableItem(menu, UnmapMItem(mFile, kStdNew));
- EnableItem(menu, UnmapMItem(mFile, kStdOpen));
- }
-
- window = FrontWindowOfType(kwIsDocument, true);
- if (window) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose));
- frHndl = (FileRecHndl)GetWRefCon(window);
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- enableItem = GetWindowDirty(window);
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- enableItem = true;
- if (enableItem)
- EnableItem(menu, UnmapMItem(mFile, kStdSave));
- EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
- }
- EnableItem(menu, UnmapMItem(mFile, kDuplicate));
- EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
- EnableItem(menu, UnmapMItem(mFile, kStdPrint));
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustEditMenu(WindowPtr window)
- {
- MenuHandle menu;
- Boolean menuEnabled;
- FileRecHndl frHndl;
-
- menu = GetMenuHandle(mEdit);
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
- EnableItem(menu, UnmapMItem(mEdit, kStdCut));
- EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
- EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
- EnableItem(menu, UnmapMItem(mEdit, kStdClear));
- return(false);
- }
-
- if (IsAppWindow(window)) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- switch ((*frHndl)->fileState.sfType) {
- #if VH_VERSION
- case kViewHierFileType:
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- #endif
- default:
- #if VH_VERSION
- EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
- #endif
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustFontsMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl;
- TEHandle teHndl;
- short i, j, mode, fnum;
- TextStyle styl;
- Str255 str;
-
- menu = GetMenuHandle(mFonts);
-
- for (i = CountMItems(menu); i; --i)
- CheckItem(menu, i, false);
-
- if (!IsAppWindow(window))
- return(false);
-
- frHndl = (FileRecHndl)GetWRefCon(window);
- teHndl = CTEFindActive(nil);
- if ((!teHndl) || (teHndl == (*frHndl)->d.doc.inBox))
- return(false);
-
- for (i = CountMItems(menu); i; --i)
- EnableItem(menu, i);
-
- mode = (doFont | doFace);
- styl.tsFace = 0x7F; /* Check out all styles. */
- TEContinuousStyle(&mode, &styl, teHndl);
- GetFontName(styl.tsFont, str);
- GetFNum(str, &styl.tsFont);
- if (mode & doFace) {
- if (!(j = styl.tsFace))
- CheckItem(menu, kPlain, true);
- else
- for (i = kBold; i <= kExtend; ++i, j >>= 1)
- if (j & 0x01)
- CheckItem(menu, i, true);
- }
- if (mode & doFont) {
- for (i = CountMItems(menu); i >= kFirstFont; --i) {
- GetMenuItemText(menu, i, str);
- GetFNum(str, &fnum);
- if (fnum == styl.tsFont) {
- CheckItem(menu, i, true);
- break;
- }
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- static short NewFontSize(short size)
- {
- short item, itype;
- Handle ihandle;
- Rect irect;
- Str255 str;
- WindowPtr oldPort, ww;
- DialogPtr dialog;
-
- GetPort(&oldPort);
-
- ww = FrontWindowOfType(kwIsDocument, true);
- dialog = GetCenteredDialog(rNewFontSizeDlg, nil, ww, (WindowPtr)-1L);
- if (dialog) {
- SetPort(dialog);
- if (size) {
- GetDialogItem(dialog, 4, &itype, &ihandle, &irect);
- pcpydec(str, size);
- SetDialogItemText(ihandle, str);
- SelectDialogItemText(dialog, 4, 0, 32767);
- }
- for (size = 0;;) {
- ModalDialog(gKeyEquivFilterUPP, &item);
- if (item == 3) break;
- if (item == 1) {
- GetDialogItem(dialog, 4, &itype, &ihandle, &irect);
- GetDialogItemText(ihandle, str);
- size = p2dec(str, nil);
- if (size < 6) size = 6;
- if (size > 128) size = 128;
- break;
- }
- }
- DisposeDialog(dialog);
- }
-
- SetPort(oldPort);
- return(size);
- }
-
-
-
-